<!-- 编辑弹窗 -->
<template>
  <!-- 高级弹窗modal -->
  <ele-modal
    form
    :width="800"
    v-model="visible"
    :title="type == 'info' ? '详情' : type == 'edit' ? '修改' : '添加'"
    @open="handleOpen"
  >
    <el-form
      ref="formRef"
      :model="form"
      :rules="rules"
      label-width="100px"
      :disabled="type=='info'"
      @submit.prevent=""
    >
      <el-row gutter="20">
        <el-col :span="8">
          <el-form-item label="摄像机名称" prop="cameraName">
            <el-input
              clearable
              v-model="form.cameraName"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="摄像机编号" prop="cameraNumber">
            <el-input
              clearable
              v-model="form.cameraNumber"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="摄像机类型" prop="cameraType">
            <dict-data code="sys_normal_disable" v-model="form.cameraType" />
          </el-form-item>
        </el-col>
    
          </el-row>
          <el-row gutter="20">
        <el-col :span="8">
          <el-form-item label="品牌" prop="cameraBrach">
            <dict-data code="sys_normal_disable" v-model="form.cameraBrach" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="型号" prop="cameraModel">
            <el-input
              clearable
              v-model="form.cameraModel"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
         </el-row>
          <el-row gutter="20">
        <el-col :span="8">
          <el-form-item label="排序号" prop="orderNum">
            <el-input-number
              :min="0"
              :max="99999"
              v-model="form.orderNum"
              placeholder="请输入"
              controls-position="right"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="状态" prop="status">
            <el-switch
              active-value="1"
              v-model="form.status"
              active-text="启用"
              inactive-text="禁用"
              inline-prompt
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row gutter="20">
        <el-col :span="24">
          <div class="title">连接配置</div>
        </el-col>
        <el-col :span="8">
          <el-form-item label="IP" prop="ip">
            <el-input clearable v-model="form.ip" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="端口" prop="port">
            <el-input-number
              :min="0"
              :max="99999"
              v-model="form.port"
              placeholder="请输入"
              controls-position="right"
            />
          </el-form-item>
        </el-col>
         </el-row>
          <el-row gutter="20">
        <el-col :span="8">
          <el-form-item label="账号" prop="username">
            <el-input clearable v-model="form.username" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="密码" prop="password">
            <el-input clearable v-model="form.password" placeholder="请输入" />
          </el-form-item>
        </el-col>
         </el-row>
          <el-row gutter="20">
        <el-col :span="16">
          <el-form-item label="RTSP流" prop="rtsp">
            <el-input clearable v-model="form.rtsp" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="支持云台控制" prop="yuntai">
            <el-switch
              active-value="1"
              v-model="form.yuntai"
              active-text="是"
              inactive-text="否"
              inline-prompt
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row gutter="20">
        <el-col :span="24">
          <div class="title">关联配置</div>
        </el-col>
        <el-col :span="8">
          <el-form-item label="关联NVR设备" prop="nvr">
            <dict-data code="sys_normal_disable" v-model="form.nvr" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleCancel">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, nextTick } from 'vue';
  import { EleMessage, emailReg, phoneReg } from 'ele-admin-plus/es';
  import { useFormData } from '@/utils/use-form-data';
  import { addDept, updateDept } from '@/api/system/dept';

  const props = defineProps({
    /** 修改回显的数据 */
    data: Object,

    type: String
  });

  const emit = defineEmits(['done']);

  /** 弹窗是否打开 */
  const visible = defineModel({ type: Boolean });

  /** 是否是修改 */
  const isUpdate = ref(false);

  /** 提交状态 */
  const loading = ref(false);

  /** 表单实例 */
  const formRef = ref(null);

  /** 表单数据 */
  const [form, resetFields, assignFields] = useFormData({
    deptId: void 0,
    parentId: void 0,
    deptName: '',
    orderNum: 0,
    leader: '',
    phone: '',
    email: '',
    status: '0'
  });

  /** 表单验证规则 */
  const rules = reactive({
    camareName: [
      {
        required: true,
        message: '请输入摄像机名称',
        type: 'string',
        trigger: 'blur'
      }
    ],
    cameraNumber: [
      {
        required: true,
        message: '请输入摄像机编号',
        type: 'number',
        trigger: 'blur'
      }
    ],
    cameraType: [
      {
        message: '请选择类型',
        trigger: 'change'
      }
    ],
    cameraBrach: [
      {
        message: '请选择品牌',
        trigger: 'change'
      }
    ],
    orderNum: [
      {
        required: true,
        message: '请输入密码',
        type: 'number',
        trigger: 'blur'
      }
    ],
    username: [
      {
        required: true,
        message: '请输入账号',
        type: 'number',
        trigger: 'blur'
      }
    ],
    password: [
      {
        required: true,
        message: '请输入排序号',
        type: 'number',
        trigger: 'blur'
      }
    ],
    ip: [
      {
        required: true,
        message: '请输入IP',
        type: 'number',
        trigger: 'blur'
      }
    ],
    port: [
      {
        required: true,
        message: '请输入端口',
        type: 'number',
        trigger: 'blur'
      }
    ]
  });

  /** 关闭弹窗 */
  const handleCancel = () => {
    visible.value = false;
  };

  /** 保存编辑 */
  const save = () => {
    formRef.value?.validate?.((valid) => {
      if (!valid) {
        return;
      }
      loading.value = true;
      const saveOrUpdate = isUpdate.value ? updateDept : addDept;
      saveOrUpdate({ ...form, parentId: form.parentId || 0 })
        .then((msg) => {
          loading.value = false;
          EleMessage.success(msg);
          handleCancel();
          emit('done');
        })
        .catch((e) => {
          loading.value = false;
          EleMessage.error(e.message);
        });
    });
  };

  /** 弹窗打开事件 */
  const handleOpen = () => {
    console.log('修改部门', props.data);
    if (props.data) {
      assignFields({
        ...props.data,
        parentId: props.data.parentId || void 0
      },true);
      isUpdate.value = true;
    } else {
      resetFields();
      form.parentId = props.parentId;
      isUpdate.value = false;
    }
    nextTick(() => {
      nextTick(() => {
        formRef.value?.clearValidate?.();
      });
    });
  };
</script>
<style scoped>
  .title {
    border-left: 2px solid #2f54eb;
    padding-left: 5px;
    line-height: 1;
    margin: 10px 0 5px;
  }
</style>
